﻿//***************************************************
//
//       File Name:    WindowMOD.cs
//
//       Author:   王文军    Email:  546527816@qq.com
//
//
//***************************************************

using System;
using System.Collections;
using System.Diagnostics;
using System.Runtime.InteropServices;
using UnityEngine;


public class WindowMOD : MonoBehaviour
{
    //2222
    //在类中调用Windows API
    //设置此窗体句柄的窗体为活动窗体
    [DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto, ExactSpelling = true)]
    public static extern IntPtr GetForegroundWindow();  //获得当前活动窗体的句柄
    [DllImport("user32.dll", EntryPoint = "SetForegroundWindow")]
    public static extern bool SetForegroundWindow(IntPtr hWnd);
    IntPtr handle;
    [DllImport("user32.dll")]
    public static extern bool ShowWindow(System.IntPtr hwnd,int nCmdShow);






    //[DllImport("user32.dll", EntryPoint = "GetForegroundWindow")]
    //public static extern System.IntPtr GetForegroundWindow();




    //1111
    //public Rect screenPosition;
    //[DllImport("user32.dll")]
    //static extern IntPtr SetWindowLong(IntPtr hwnd, int _nIndex, int dwNewLong);
    //[DllImport("user32.dll")]
    //static extern bool SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
    //[DllImport("user32.dll")]
    //static extern IntPtr GetForegroundWindow();
    //const uint SWP_SHOWWINDOW = 0x0040;
    //const int GWL_STYLE = -16;
    //const int WS_BORDER = 1;
    //const int WS_POPUP = 0x800000;



    //    //3333
    //[DllImport("user32.dll")]
    //static extern bool CloseWindow(IntPtr hwnd);
    //[DllImport("user32.dll", EntryPoint = "FindWindow")]
    //static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
    //ParenthWnd = new IntPtr(0);
    //ParenthWnd = FindWindow(null, "d2");//这里的d2是我的窗口名字


    void Awake()
    {
        //调用2222
        handle = GetForegroundWindow();

        //3333
       // handle = FindWindow(null, "设计端");



    }

    void Start()
    {
        //1111
        //SetWindowLong(GetForegroundWindow(), GWL_STYLE, WS_POPUP);//将网上的WS_BORDER替换成WS_POPUP  
        //bool result = SetWindowPos(GetForegroundWindow(), -1, (int)screenPosition.x,
        //    (int)screenPosition.y, (int)screenPosition.width, (int)screenPosition.height, SWP_SHOWWINDOW);

        QualitySettings.SetQualityLevel(5);
        Screen.SetResolution(1920, 1080, true);
        //Screen.fullScreen = true;
        
    }
    
    void Update()
    {
       //// 2222
        if (handle != GetForegroundWindow()) //获取当前活动窗体
        {
            ShowWindow(handle, 4);//窗口最大化
            SetForegroundWindow(handle); //如果不是,强制把自己的设置为活动窗体
        }
        //if (Screen.fullScreen != true)
        //{
        //    Screen.fullScreen = true;
        //}
    } 

    //void OnGUI()
    //{
    //    GUILayout.Label(handle.ToString());
    //}
    
    //1111
    //#region DllImports
    //[DllImport("user32.dll")]
    //public static extern int WindowFromPoint(int xPoint, int yPoint);

    //[DllImport("user32.dll")]
    //public static extern int SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int y, int Width, int Height, int flags);

    ////参数分别是：x,y分别表示窗口左上角在屏幕的什么位置，width，height为0表示不改变窗口大小。

    //[DllImport("user32.dll")]
    //public static extern int GetForegroundWindow();

    //[DllImport("user32.dll")]
    //public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

    //[DllImport("user32.dll")]
    //public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

    //[DllImport("user32.dll")]
    //public static extern int GetWindowText(int hwnd, string lpString, int cch);

    //[DllImport("user32.dll")]
    //public static extern IntPtr GetActiveWindow();

    //[DllImport("user32.dll")]
    //public static extern bool SetForegroundWindow(IntPtr hWnd);

    //[DllImport("user32.dll")]
    //public static extern long SetWindowLong(IntPtr hWnd, int nIndex, long dwNewLong);
    //#endregion
    //IEnumerator forceRes()
    //{
    //    yield return new WaitForSeconds(2);
    //    Screen.SetResolution(3840, 1620, false);
    //    yield return new WaitForSeconds(2);
    //    SetWindowPos(GetActiveWindow(), -1, 0, 0, 0, 0, 0x001);
    //}


}







/// <summary>
/// 设置窗体类
/// </summary>
public class TopMostWindow
{
    public const int HWND_TOP = 0;
    public const int HWND_BOTTOM = 1;
    public const int HWND_TOPMOST = -1;
    public const int HWND_NOTOPMOST = -2;

    [DllImport("user32.dll")]
    public static extern IntPtr SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, uint wFlags);

    [DllImport("user32.dll")]
    public static extern bool GetWindowRect(IntPtr hWnd, out WindowRect lpRect);
    /// <summary> 
    /// 设置窗体为TopMost 
    /// </summary> 
    /// <param name="hWnd"></param> 
    public static void SetTopomost(IntPtr hWnd)
    {
        WindowRect rect = new WindowRect();
        GetWindowRect(hWnd, out rect);
        SetWindowPos(hWnd, (IntPtr)HWND_TOPMOST, rect.Left, rect.Top, rect.Right - rect.Left, rect.Bottom - rect.Top, 0);
    }
}
public struct WindowRect
{
    public int Left;
    public int Top;
    public int Right;
    public int Bottom;
} 
